/*
 * @: 
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-11-02 13:56:25
 * @LastEditors: ziwei
 * @LastEditTime: 2021-11-02 17:49:45
 */
//执行saga
import runSaga from "./runSaga";
import createChannel from "./createChannel";

function createSagaMiddleware(params) {
  let channel = createChannel();
  let boundRunSaga;
  function sagaMiddleware({getState,dispatch}) {
    let env = {dispatch,getState,channel}
    boundRunSaga = runSaga.bind(null,env)
    return function (next) {
      //重写了dispatch方法 
      return function (action) {
        //往后走
        const result = next(action);
        channel.emit(action);
        return result
      }
    }
  }
  sagaMiddleware.run = (saga)=>boundRunSaga(saga)
  return sagaMiddleware
}

export default createSagaMiddleware